﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;    //数据库头文件
using System.Data.Sql;         //数据库头文件
using DATA_operate_;          //自定义数据库操作类引用

public partial class login : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
          SqlInject myCheck = new SqlInject(this.Request);
        myCheck.CheckSqlInject();

    }
 
    protected void Button2_Click(object sender, EventArgs e)
    {
        DATA_operate data_operate_in_login = new DATA_operate();   //创建数据库操作类对象
        string teaid, stuid, aid;
        string user_typr_choose;   //单选框上显示的用户类型
        int user_type = 0;        //用户类型     学生：1     老师：2     管理员：3
        string Sql_user_type;    //登录验证sql语句
        user_typr_choose = ((RadioButtonList)this.FindControl("RadioButtonList1")).SelectedValue.ToString();//获得登录用户类型
        switch(user_typr_choose)
        {
            case "学生":
                if (data_operate_in_login.Is_succeed_login("_student", "_Sid", TextBox1.Text, "_Spwd", TextBox2.Text))  //进行登录验证
                {
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('登录成功。你是学生');", true);
                    user_type = 1;    //成功。这标志用户类型为1    学生    否者都不成功则继续为0
                }
                
                break;
            case "老师":
                if (data_operate_in_login.Is_succeed_login("_teacher", "_Tid", TextBox1.Text, "_Tpwn", TextBox2.Text))
                user_type = 2;
                break;
            case "管理员":
                if (data_operate_in_login.Is_succeed_login("_admin", "_Aid", TextBox1.Text, "_Apwn", TextBox2.Text))
                user_type = 3;
                break;            
        }

        stuid = this.TextBox1.Text.Trim();
        aid = this.TextBox1.Text.Trim();
        teaid = this.TextBox1.Text.Trim();
        //登录成功后的跳转和其他操作--------------------------------
        switch(user_type)
        {
            case 0:        //登录失败
               
                Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('用户名或密码错误');", true);//弹出提示框
                break;
            case 1:       //登录成功----学生
                Response.SetCookie(new HttpCookie("stuid", stuid));
                 SqlDataReader reader;
                 SqlCommand com = data_operate_in_login.sqlcmd("select * from _tran_post where _receiver_id='" + stuid + "' and _post_name = '***未交作业提醒消息***' and _read_count='0'");
                 reader = com.ExecuteReader();   //判断返回
                 bool Is_succeed = reader.Read();
                 if (Is_succeed)                  //已经存在
                 {
   
                     Response.Write("<script>alert('你有未完成作业消息提醒。请及时查看。并完成作业');window.location.href='../student/student_index.aspx'</script> ");
                 } 
                 else                         //不存在
                 {
                     Server.Transfer("../student/student_index.aspx", true);          //跳转到学生首页
                 }       
                break;
            case 2:       //登录成功----老师
                Response.SetCookie(new HttpCookie("teaid", teaid));
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('登录成功。你是老师');", true);
                Server.Transfer("../teacher/teacher_index.aspx", true);               //跳转到老师首页
                break;
            case 3:        //登录成功----管理员
                Response.SetCookie(new HttpCookie("aid", aid));
               // Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('登录成功。你是管理员');", true);
                Server.Transfer("../admin/admin_index.aspx", true);               //跳转到管理员首页
                break;
        }
        //--------------------------------------------------------------------
      
    }

    protected void Button3_Click(object sender, EventArgs e)           //重置所有选框信息
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
    }
}